package com.wangxiang.shop.product.service.impl;

import com.wangxiang.shop.product.dao.*;
import com.wangxiang.shop.product.entity.HttpAddEntity;
import com.wangxiang.shop.product.entity.ShopBaseEntity;
import com.wangxiang.shop.product.entity.ShopTwoTypeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxiang.common.utils.PageUtils;
import com.wangxiang.common.utils.Query;

import com.wangxiang.shop.product.entity.ShopThreeTypeEntity;
import com.wangxiang.shop.product.service.ShopThreeTypeService;


@Service("shopThreeTypeService")
public class ShopThreeTypeServiceImpl extends ServiceImpl<ShopThreeTypeDao, ShopThreeTypeEntity> implements ShopThreeTypeService {
    @Autowired
    private ShopThreeTypeDao shopThreeTypeDao;
    @Autowired
    private TwoThreeTypeRelDao twoThreeTypeRelDao;
    @Autowired
    private ShopBaseDao shopBaseDao;
    @Autowired
    private BaseTwoRelDao baseTwoRelDao;
    @Autowired
    private ShopTwoTypeDao shopTwoTypeDao;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShopThreeTypeEntity> page = this.page(
                new Query<ShopThreeTypeEntity>().getPage(params),
                new QueryWrapper<ShopThreeTypeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean getEntity(List<Integer> list, String name) {
        if (list.size()==0){
            //第一个商品
            return false;
        }

        for (int i = 0; i < list.size(); i++) {
            ShopThreeTypeEntity shopThreeType = shopThreeTypeDao.selectById(list.get(i));
            if (shopThreeType.getName().equals(name)){
                return true;
            }
        }


        return false;
    }

    @Override
    public boolean add(HttpAddEntity addEntity) {
        //先插入实体
        ShopThreeTypeEntity shopThreeTypeEntity = new ShopThreeTypeEntity();
        String name = addEntity.getName();
        shopThreeTypeEntity.setName(name);
        int i = shopThreeTypeDao.insertGetKey(shopThreeTypeEntity);
        if (i>0){
            int i1 = twoThreeTypeRelDao.addAssociation(addEntity.getParentId(), shopThreeTypeEntity.getId());
            if (i1>0){
                return true;
            }
        }

        return false;
    }

    /**
     * 三级分类
     * @return
     */
    @Override
    public List<ShopBaseEntity> category() {
        //查询第一级，有效的基础分类
        List<ShopBaseEntity> baseEntityList = shopBaseDao.getBaseEntity();
        ArrayList<ShopBaseEntity> list = new ArrayList<>();
        //更具baseId查询关系表
        baseEntityList.forEach((baseItem)->{
            ShopBaseEntity shopBaseEntity = baseItem;
            //查关系表，base_two_type_rel
            List<Integer> twoIdList = baseTwoRelDao.getChildrenId(baseItem.getId());
            ArrayList<ShopTwoTypeEntity> twoEntityList = new ArrayList<>();
            twoIdList.forEach((twoId)->{
                //查二级商品,有效的
                ShopTwoTypeEntity shopTwoTypeEntity = shopTwoTypeDao.getStatusById(twoId);
                twoEntityList.add(shopTwoTypeEntity);

                //查询关系表two_three_type_rel
                List<Integer> threeIdList = twoThreeTypeRelDao.getChildrenId(twoId);
                ArrayList<ShopThreeTypeEntity> threeEntity = new ArrayList<>();

                threeIdList.forEach((threeId)->{
                    ShopThreeTypeEntity statusById = shopThreeTypeDao.getStatusById(threeId);
                    threeEntity.add(statusById);
                });
                shopTwoTypeEntity.setChildren(threeEntity);


            });
            shopBaseEntity.setChildren(twoEntityList);
//            System.out.println(shopBaseEntity);
            list.add(shopBaseEntity);


        });



        return list;
    }

}